#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QColorDialog>
#include <QSpinBox>
#include <QCheckBox>
#include <QGroupBox>
#include <QSlider>
#include <QMessageBox>
#include "roundmenuwidget.h"

class MainWindow : public QWidget
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr)
        : QWidget(parent)
    {
        setupUI();
        setupConnections();
        setupDemoMenu();
    }

private slots:
    void onMenuItemClicked(int index)
    {
        QMessageBox::information(this, "菜单项点击", QString("点击了菜单项 %1").arg(index + 1));
    }

    void onMenuToggled(bool expanded)
    {
        statusLabel->setText(expanded ? "菜单已展开" : "菜单已收起");
    }

    void onStyleChanged()
    {
        MenuStyleConfig style = roundMenu->getMenuStyle();
        
        // 更新颜色
        style.centerButtonColor = centerColorBtn->palette().color(QPalette::Button);
        style.menuItemBackgroundColor = itemColorBtn->palette().color(QPalette::Button);
        style.menuItemHoverColor = hoverColorBtn->palette().color(QPalette::Button);
        
        // 更新选项
        style.showBackground = showBgCheck->isChecked();
        style.showSeparatorLines = showLinesCheck->isChecked();
        style.useRoundMenuItems = roundItemsCheck->isChecked();
        style.enableRotationAnimation = rotationAnimCheck->isChecked();
        style.useRandomColors = randomColorsCheck->isChecked();
        
        // 更新数值
        style.animationDuration = animDurationSpin->value();
        style.outerRadiusScale = outerRadiusSlider->value() / 100.0;
        style.innerRadiusScale = innerRadiusSlider->value() / 100.0;
        
        roundMenu->setMenuStyle(style);
    }

    void addMenuItem()
    {
        static int itemCount = 0;
        QString title = QString("项目%1").arg(++itemCount);
        roundMenu->addMenuItem(title, QString("工具提示 %1").arg(itemCount));
    }

    void removeMenuItem()
    {
        if (roundMenu->menuItemCount() > 0) {
            roundMenu->removeMenuItem(roundMenu->menuItemCount() - 1);
        }
    }

    void clearMenuItems()
    {
        roundMenu->clearMenuItems();
    }    void chooseColor(QPushButton* button)
    {
        QColor color = QColorDialog::getColor(button->palette().color(QPalette::Button), this);
        if (color.isValid()) {
            QPalette palette = button->palette();
            palette.setColor(QPalette::Button, color);
            button->setPalette(palette);
            button->setAutoFillBackground(true);
            onStyleChanged();
        }
    }

    void onEffectChanged()
    {
        MenuStyleConfig style = roundMenu->getMenuStyle();
        style.enableRippleEffect = rippleEffectCheck->isChecked();
        style.enableGlowEffect = glowEffectCheck->isChecked();
        style.enableParticleEffect = particleEffectCheck->isChecked();
        roundMenu->setMenuStyle(style);
    }

    void applyPreset1()  // 经典预设
    {
        MenuStyleConfig style = roundMenu->getMenuStyle();
        style.centerButtonColor = QColor(70, 130, 180);
        style.menuItemBackgroundColor = QColor(100, 100, 100, 100);
        style.menuItemHoverColor = QColor(150, 150, 150, 150);
        style.useRoundMenuItems = true;
        style.enableRotationAnimation = true;
        style.enableRippleEffect = false;
        style.enableGlowEffect = false;
        roundMenu->setMenuStyle(style);
        updateControlsFromStyle(style);
    }

    void applyPreset2()  // 炫酷预设
    {
        MenuStyleConfig style = roundMenu->getMenuStyle();
        style.centerButtonColor = QColor(255, 20, 147);
        style.menuItemBackgroundColor = QColor(138, 43, 226, 120);
        style.menuItemHoverColor = QColor(255, 105, 180, 180);
        style.useRoundMenuItems = true;
        style.enableRotationAnimation = true;
        style.enableRippleEffect = true;
        style.enableGlowEffect = true;
        style.useRandomColors = true;
        roundMenu->setMenuStyle(style);
        updateControlsFromStyle(style);
    }

    void applyPreset3()  // 简约预设
    {
        MenuStyleConfig style = roundMenu->getMenuStyle();
        style.centerButtonColor = QColor(220, 220, 220);
        style.menuItemBackgroundColor = QColor(240, 240, 240, 200);
        style.menuItemHoverColor = QColor(200, 200, 200, 200);
        style.useRoundMenuItems = false;
        style.enableRotationAnimation = false;
        style.enableRippleEffect = false;
        style.enableGlowEffect = false;
        style.useRandomColors = false;
        roundMenu->setMenuStyle(style);
        updateControlsFromStyle(style);
    }

private:
    void updateControlsFromStyle(const MenuStyleConfig& style)
    {
        // 更新颜色按钮
        setPalette(centerColorBtn, style.centerButtonColor);
        setPalette(itemColorBtn, style.menuItemBackgroundColor);
        setPalette(hoverColorBtn, style.menuItemHoverColor);
        
        // 更新复选框
        showBgCheck->setChecked(style.showBackground);
        showLinesCheck->setChecked(style.showSeparatorLines);
        roundItemsCheck->setChecked(style.useRoundMenuItems);
        rotationAnimCheck->setChecked(style.enableRotationAnimation);
        randomColorsCheck->setChecked(style.useRandomColors);
        
        // 更新特效复选框
        rippleEffectCheck->setChecked(style.enableRippleEffect);
        glowEffectCheck->setChecked(style.enableGlowEffect);
        particleEffectCheck->setChecked(style.enableParticleEffect);
        
        // 更新数值控件
        animDurationSpin->setValue(style.animationDuration);
        outerRadiusSlider->setValue(static_cast<int>(style.outerRadiusScale * 100));
        innerRadiusSlider->setValue(static_cast<int>(style.innerRadiusScale * 100));
    }

    void setupUI()
    {
        setWindowTitle("圆形旋转菜单演示");
        setMinimumSize(800, 600);
        
        // 主布局
        QHBoxLayout *mainLayout = new QHBoxLayout(this);
        
        // 左侧菜单区域
        QWidget *menuWidget = new QWidget;
        menuWidget->setMinimumSize(400, 400);
        QVBoxLayout *menuLayout = new QVBoxLayout(menuWidget);
        
        roundMenu = new RoundMenuWidget(menuWidget);
        roundMenu->setMinimumSize(350, 350);
        
        statusLabel = new QLabel("菜单状态：未展开");
        statusLabel->setAlignment(Qt::AlignCenter);
        
        menuLayout->addWidget(roundMenu);
        menuLayout->addWidget(statusLabel);
        menuLayout->addStretch();
        
        // 右侧控制面板
        QWidget *controlWidget = new QWidget;
        controlWidget->setMaximumWidth(350);
        setupControlPanel(controlWidget);
        
        mainLayout->addWidget(menuWidget);
        mainLayout->addWidget(controlWidget);
    }

    void setupControlPanel(QWidget *parent)
    {
        QVBoxLayout *layout = new QVBoxLayout(parent);
        
        // 菜单项控制
        QGroupBox *itemGroup = new QGroupBox("菜单项控制");
        QVBoxLayout *itemLayout = new QVBoxLayout(itemGroup);
        
        QPushButton *addBtn = new QPushButton("添加菜单项");
        QPushButton *removeBtn = new QPushButton("删除菜单项");
        QPushButton *clearBtn = new QPushButton("清空菜单");
        
        connect(addBtn, &QPushButton::clicked, this, &MainWindow::addMenuItem);
        connect(removeBtn, &QPushButton::clicked, this, &MainWindow::removeMenuItem);
        connect(clearBtn, &QPushButton::clicked, this, &MainWindow::clearMenuItems);
        
        itemLayout->addWidget(addBtn);
        itemLayout->addWidget(removeBtn);
        itemLayout->addWidget(clearBtn);
        
        // 颜色控制
        QGroupBox *colorGroup = new QGroupBox("颜色设置");
        QVBoxLayout *colorLayout = new QVBoxLayout(colorGroup);
        
        centerColorBtn = new QPushButton("中心按钮颜色");
        itemColorBtn = new QPushButton("菜单项颜色");
        hoverColorBtn = new QPushButton("悬停颜色");
        
        // 设置初始颜色
        setPalette(centerColorBtn, QColor(144, 133, 227));
        setPalette(itemColorBtn, QColor(77, 77, 77, 44));
        setPalette(hoverColorBtn, QColor(192, 192, 192, 144));
        
        connect(centerColorBtn, &QPushButton::clicked, [this]() { chooseColor(centerColorBtn); });
        connect(itemColorBtn, &QPushButton::clicked, [this]() { chooseColor(itemColorBtn); });
        connect(hoverColorBtn, &QPushButton::clicked, [this]() { chooseColor(hoverColorBtn); });
        
        colorLayout->addWidget(centerColorBtn);
        colorLayout->addWidget(itemColorBtn);
        colorLayout->addWidget(hoverColorBtn);
        
        // 显示选项
        QGroupBox *optionGroup = new QGroupBox("显示选项");
        QVBoxLayout *optionLayout = new QVBoxLayout(optionGroup);
        
        showBgCheck = new QCheckBox("显示背景");
        showLinesCheck = new QCheckBox("显示分割线");
        roundItemsCheck = new QCheckBox("圆形菜单项");
        rotationAnimCheck = new QCheckBox("旋转动画");
        randomColorsCheck = new QCheckBox("随机颜色");
        
        roundItemsCheck->setChecked(true);
        rotationAnimCheck->setChecked(true);
        
        connect(showBgCheck, &QCheckBox::toggled, this, &MainWindow::onStyleChanged);
        connect(showLinesCheck, &QCheckBox::toggled, this, &MainWindow::onStyleChanged);
        connect(roundItemsCheck, &QCheckBox::toggled, this, &MainWindow::onStyleChanged);
        connect(rotationAnimCheck, &QCheckBox::toggled, this, &MainWindow::onStyleChanged);
        connect(randomColorsCheck, &QCheckBox::toggled, this, &MainWindow::onStyleChanged);
        
        optionLayout->addWidget(showBgCheck);
        optionLayout->addWidget(showLinesCheck);
        optionLayout->addWidget(roundItemsCheck);
        optionLayout->addWidget(rotationAnimCheck);
        optionLayout->addWidget(randomColorsCheck);
        
        // 数值设置
        QGroupBox *valueGroup = new QGroupBox("数值设置");
        QVBoxLayout *valueLayout = new QVBoxLayout(valueGroup);
        
        // 动画时长
        QLabel *animLabel = new QLabel("动画时长 (ms):");
        animDurationSpin = new QSpinBox;
        animDurationSpin->setRange(100, 2000);
        animDurationSpin->setValue(400);
        animDurationSpin->setSingleStep(100);
        connect(animDurationSpin, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::onStyleChanged);
        
        // 外圆半径
        QLabel *outerLabel = new QLabel("外圆半径比例:");
        outerRadiusSlider = new QSlider(Qt::Horizontal);
        outerRadiusSlider->setRange(50, 90);
        outerRadiusSlider->setValue(75);
        QLabel *outerValueLabel = new QLabel("75%");
        connect(outerRadiusSlider, &QSlider::valueChanged, [this, outerValueLabel](int value) {
            outerValueLabel->setText(QString("%1%").arg(value));
            onStyleChanged();
        });
        
        // 内圆半径
        QLabel *innerLabel = new QLabel("内圆半径比例:");
        innerRadiusSlider = new QSlider(Qt::Horizontal);
        innerRadiusSlider->setRange(20, 60);
        innerRadiusSlider->setValue(40);
        QLabel *innerValueLabel = new QLabel("40%");
        connect(innerRadiusSlider, &QSlider::valueChanged, [this, innerValueLabel](int value) {
            innerValueLabel->setText(QString("%1%").arg(value));
            onStyleChanged();
        });
        
        valueLayout->addWidget(animLabel);
        valueLayout->addWidget(animDurationSpin);
        valueLayout->addWidget(outerLabel);
        valueLayout->addWidget(outerRadiusSlider);
        valueLayout->addWidget(outerValueLabel);
        valueLayout->addWidget(innerLabel);        valueLayout->addWidget(innerRadiusSlider);
        valueLayout->addWidget(innerValueLabel);
        
        // 特效控制组
        QGroupBox *effectGroup = new QGroupBox("特效设置");
        QVBoxLayout *effectLayout = new QVBoxLayout(effectGroup);
        
        rippleEffectCheck = new QCheckBox("波纹效果");
        glowEffectCheck = new QCheckBox("发光效果");
        particleEffectCheck = new QCheckBox("粒子效果");
        
        connect(rippleEffectCheck, &QCheckBox::toggled, this, &MainWindow::onEffectChanged);
        connect(glowEffectCheck, &QCheckBox::toggled, this, &MainWindow::onEffectChanged);
        connect(particleEffectCheck, &QCheckBox::toggled, this, &MainWindow::onEffectChanged);
        
        QPushButton *presetBtn1 = new QPushButton("预设1: 经典");
        QPushButton *presetBtn2 = new QPushButton("预设2: 炫酷");
        QPushButton *presetBtn3 = new QPushButton("预设3: 简约");
        
        connect(presetBtn1, &QPushButton::clicked, this, &MainWindow::applyPreset1);
        connect(presetBtn2, &QPushButton::clicked, this, &MainWindow::applyPreset2);
        connect(presetBtn3, &QPushButton::clicked, this, &MainWindow::applyPreset3);
        
        effectLayout->addWidget(rippleEffectCheck);
        effectLayout->addWidget(glowEffectCheck);
        effectLayout->addWidget(particleEffectCheck);
        effectLayout->addWidget(presetBtn1);
        effectLayout->addWidget(presetBtn2);
        effectLayout->addWidget(presetBtn3);
        
        // 添加到主布局
        layout->addWidget(itemGroup);
        layout->addWidget(colorGroup);
        layout->addWidget(optionGroup);
        layout->addWidget(valueGroup);
        layout->addWidget(effectGroup);
        layout->addStretch();
    }

    void setupConnections()
    {
        connect(roundMenu, &RoundMenuWidget::menuItemClicked, this, &MainWindow::onMenuItemClicked);
        connect(roundMenu, &RoundMenuWidget::menuToggled, this, &MainWindow::onMenuToggled);
    }    void setupDemoMenu()
    {
        // 设置中心按钮图标
        roundMenu->setExpandIcon(":/icons/icons/menu_expand.svg");
        roundMenu->setCollapseIcon(":/icons/icons/menu_collapse.svg");
        
        // 添加带图标的菜单项
        QPixmap homeIcon(":/icons/icons/home.svg");
        QPixmap settingsIcon(":/icons/icons/settings.svg");
        QPixmap helpIcon(":/icons/icons/help.svg");
        QPixmap addIcon(":/icons/icons/add.svg");
        QPixmap exitIcon(":/icons/icons/exit.svg");
        
        roundMenu->addMenuItem("首页", "返回首页", homeIcon, QColor(100, 150, 200, 120));
        roundMenu->addMenuItem("设置", "打开设置", settingsIcon, QColor(150, 100, 200, 120));
        roundMenu->addMenuItem("帮助", "查看帮助", helpIcon, QColor(100, 200, 150, 120));
        roundMenu->addMenuItem("添加", "添加新项目", addIcon, QColor(200, 150, 100, 120));
        roundMenu->addMenuItem("退出", "退出程序", exitIcon, QColor(200, 100, 100, 120));
        roundMenu->addMenuItem("工具", "工具箱", QPixmap(), QColor(150, 150, 150, 120));
    }

    void setPalette(QPushButton* button, const QColor& color)
    {
        QPalette palette = button->palette();
        palette.setColor(QPalette::Button, color);
        button->setPalette(palette);
        button->setAutoFillBackground(true);
    }

private:
    RoundMenuWidget *roundMenu;
    QLabel *statusLabel;
    
    // 控制控件
    QPushButton *centerColorBtn;
    QPushButton *itemColorBtn;
    QPushButton *hoverColorBtn;
    
    QCheckBox *showBgCheck;
    QCheckBox *showLinesCheck;
    QCheckBox *roundItemsCheck;
    QCheckBox *rotationAnimCheck;
    QCheckBox *randomColorsCheck;
    QSpinBox *animDurationSpin;
    QSlider *outerRadiusSlider;
    QSlider *innerRadiusSlider;
    
    // 特效控件
    QCheckBox *rippleEffectCheck;
    QCheckBox *glowEffectCheck;
    QCheckBox *particleEffectCheck;
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    
    MainWindow window;
    window.show();
    
    return app.exec();
}

#include "main.moc"
